

class Resource:
    def __enter__(self):
        print('connected to resource')
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('disconnected to resource')
        return True

    def query(self):
        print('querying...')


with Resource() as res:
    1/0
    res.query()
